package org.record.mood.web.controller.admin;

import lombok.extern.slf4j.Slf4j;
import org.record.mood.anocation.Log;
import org.record.mood.bean.Login;
import org.record.mood.common.*;
import org.record.mood.enums.LogTarget;
import org.record.mood.service.LoginService;
import org.record.mood.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @description:
 * @author: sunyw
 * @time: 2020-04-16 10:54
 */
@Controller
@RequestMapping("/admin")
@Slf4j
public class adminController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private MessageService messageService;

    /**
     * 登录
     *
     * @param model
     * @param login
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public CommonResp login(Model model, Login login, HttpSession session) {
        if (RecordSUtils.isBlank(login)) {
            return CommonResp.error("登录信息有误哦~~");
        }
        CommonResp resp = loginService.login(login);
        if (CommonEnum.SUCCESS.getCode().equals(resp.getCode())) {
            //赋予session 跳转到后台首页
            session.setAttribute(RecordConst.USER_LOGIN_TOCKEN, login);
        }
        log.info("返回信息为[{}]", resp);
        return resp;
    }

    /**
     * 后台首页
     *
     * @param model
     * @return
     */
    @RequestMapping("/management")
    public String managementIndex(Model model) {
        /*发布文章数量*/
        String countMessage = messageService.countMessage();
        /*友链数量*/
        String countFriends = messageService.countFriends();
        /*成立天数*/
        Login login = messageService.countForCreate();
        model.addAttribute("countMessage", countMessage);
        model.addAttribute("countFriends", countFriends);
        model.addAttribute("createDays", DateUtils.days(login.getCreateTime()));
        model.addAttribute("username",login.getUsername());
        return "admin/indexpage";
    }

    /**
     * 跳转到登录界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String admin(Model model, HttpSession session) {
        Object o = session.getAttribute(RecordConst.USER_LOGIN_TOCKEN);
        if (RecordSUtils.isNotBlank(o)) {
            //跳转到后台首页
            return "redirect:/admin/management";
        }
        return "admin/loginpage";
    }

    /**
     * 登出
     *
     * @param session
     * @return
     */
    @RequestMapping("/exit")
    public String loginOut(HttpSession session) {
        session.invalidate();
        return "admin/loginpage";
    }

    /**
     * 注册
     */
    @PostMapping("register")
    @ResponseBody
    public CommonResp register(Model model, Login login) {
        if (RecordSUtils.isBlank(login)) {
            return CommonResp.error("注册信息有误哦~.~");
        }
        return loginService.register(login);
    }

    /**
     * 注册页面跳转
     *
     * @return
     */
    @RequestMapping("/registerpage")
    public String registerPage() {
        return "admin/register";
    }
}
